/* -------- Calendar By Desmond ------------------ */

Date.prototype.format = function(style) {
  var o = {
    "M+" : this.getMonth() + 1, //month
    "d+" : this.getDate(),      //day
    "h+" : this.getHours(),     //hour
    "m+" : this.getMinutes(),   //minute
    "s+" : this.getSeconds(),   //second
    "w+" : "天一二三四五六".charAt(this.getDay()),   //week
    "q+" : Math.floor((this.getMonth() + 3) / 3),  //quarter
    "S"  : this.getMilliseconds() //millisecond
  }
  if(/(y+)/.test(style)) {
    style = style.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for(var k in o){
    if(new RegExp("("+ k +")").test(style)){
      style = style.replace(RegExp.$1,
        RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return style;
};


var DateChooser = Class.create(); 

DateChooser.language = {
	txtyear   : [["年"], [""]],
	txtmonths : [["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
       			["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]],
	txtweeks  : [["日","一","二","三","四","五","六"],
        		["SUN","MON","TUES","WED","THU","FRI","SAT"]],
	txtweeks_l: [["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
				["SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"]],
	txtclear  : [["清空"], ["CLS"]],
	txttoday  : [["今天"], ["TODAY"]],
	txtclose  : [["关闭"], ["CLOSE"]]
}
DateChooser.classes = {
	ctable:"calendarTable",
	head:"date_normal_head",
	sunhead:"date_sun_head",
	sunbody:"date_sun_body",
	sathead:"date_sat_head",
	satbody:"date_sat_body",
	norbody:"date_normal_body",
	seldate:"date_select",
	todate:"date_today",
	btnext:"bt_next",
	btprev:"bt_prev",
	cbar:"controlBar"
}

DateChooser.prototype = {
	initialize: function(id,crtDate,dClass,beginYear, endYear, lang,islong, dateFormatStyle){
		this.id = id;
		this.beginYear 	= beginYear || 1999;
		this.endYear 	= endYear || 2020;
		this.lang 		= lang!=null?lang:0;    //0(中文) | 1(英文)
		this.dateFormatStyle = dateFormatStyle || "yyyy-MM-dd";
		this.islong 	= islong!=null?islong:false;
		this.dateControl= null;
		this.dbody 		= null;
		
		this.today 	= new Date();
		this.value 	= crtDate || null;
		this.date 	= this.value?this.value.toDate():new Date();
		this.year 	= this.date.getFullYear();
		this.month 	= this.date.getMonth();
		
		this.container 		= document.createElement("div");
		this.container.id 	= this.id;
		this.container.className = dClass || "";
		this.btprev 		= null;
		this.btnext 		= null;
		this.selyear 		= null;
		this.selmonth 		= null;		
		
		this.drawHead();
		this.drawBody();
		
		this.bindYear();
		this.bindMonth();
		this.bindData();
		this.changeSelect();
	},
	drawHead :function(){		
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		
		table.width = "100%";
		table.setAttribute("cellPadding",0);
		table.setAttribute("cellSpacing",0);
	
		this.btprev 	= document.createElement("input");
		this.btnext 	= document.createElement("input");
		this.selyear 	= document.createElement("select");
		this.selmonth 	= document.createElement("select");
		
		this.btprev.onclick 	= function(event){this.goPrevMonth(event);}.bind(this);
		this.btnext.onclick 	= function(event){this.goNextMonth(event);}.bind(this);
		this.selyear.onchange 	= function(event){this.update(event);}.bind(this);
		this.selmonth.onchange 	= function(event){this.update(event);}.bind(this);
		
		this.btprev.type 		= this.btnext.type = "button";
		this.btprev.className 	= DateChooser.classes.btprev;
		this.btnext.className 	= DateChooser.classes.btnext;
		
		var tr 	= document.createElement("tr");
		var td1 = document.createElement("td");
		var td2 = document.createElement("td");
		var td3 = document.createElement("td");	
		td1.align = td2.align = td3.align = "center";
		
		td1.insertBefore(this.btprev,null);
		td2.insertBefore(this.selyear,null);
		td2.insertBefore(this.selmonth,null);
		td3.insertBefore(this.btnext,null);
		
		tr.insertBefore(td1,null);
		tr.insertBefore(td2,null);
		tr.insertBefore(td3,null);
		tr.className = DateChooser.classes.cbar;
		
		tbody.insertBefore(tr,null);
		table.insertBefore(tbody,null);
		this.container.insertBefore(table,null);
		
		
		
	},
	drawBody :function(){		
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		var title = document.createElement("tr");		
		
		table.width = "100%";
		table.setAttribute("cellPadding",0);
		table.setAttribute("cellSpacing",1);
		table.className = DateChooser.classes.ctable;
		
		for(var j=0;j<7;j++){
			var th = document.createElement("th");
			var txt = this.islong?DateChooser.language.txtweeks_l[this.lang][j]:DateChooser.language.txtweeks[this.lang][j];
			th.insertBefore(document.createTextNode(txt),null);
			if(j==0){
				th.className = DateChooser.classes.sunhead;
			}else if(j==6){
				th.className = DateChooser.classes.sathead;
			}else{
				th.className = DateChooser.classes.head;
			}			
			title.insertBefore(th,null);			
		}
		tbody.insertBefore(title,null);
			
		for(var i=0;i<6;i++){			
			var tr = document.createElement("tr");
			for(var j=0;j<7;j++){
				var td = document.createElement("td");
				if(j==0){
					td.className = DateChooser.classes.sunbody;
				}else if(j==6){
					td.className = DateChooser.classes.satbody;
				}else{
					td.className = DateChooser.classes.norbody;
				}
				tr.insertBefore(td,null);
			}
			tbody.insertBefore(tr,null);
		}
		this.dbody = tbody;
		table.insertBefore(tbody,null);
		this.container.insertBefore(table,null);
	},
	bindYear : function() {
		var sy = this.selyear;
		sy.length = 0;
		for (var i = this.beginYear; i <= this.endYear; i++){
			sy.options[sy.length] = new Option(i + DateChooser.language.txtyear[this.lang], i);
		}
	},
	bindMonth : function() {
		var cm = this.selmonth;
		cm.length = 0;
		for (var i = 0; i < 12; i++){
			cm.options[cm.length] = new Option(DateChooser.language.txtmonths[this.lang][i], i);
		}
	},
	goPrevMonth : function(e){
		if (this.year == this.beginYear && this.month == 0){return;}
			this.month--;
			if (this.month == -1) {
			this.year--;
			this.month = 11;
		}
		this.date = new Date(this.year, this.month, 1);
		this.changeSelect();
		this.bindData();
	},
	goNextMonth : function(e){
		if (this.year == this.endYear && this.month == 11){return;}
		this.month++;
		if (this.month == 12) {
			this.year++;
			this.month = 0;
		}
		this.date = new Date(this.year, this.month, 1);
		this.changeSelect();
		this.bindData();
	},
	getDaysByMonth : function (y, m) {
		var arr = [];
		var firstDay = new Date(y, m, 1).getDay();
		var daysInMonth = new Date(y, m+1 , 0).getDate();
		for (var i = 0; i < 42; i++) {
			arr[i] = "";
		}
		for (var i = 0; i < daysInMonth; i++){
			arr[i + firstDay] = i + 1;
		}
		return arr;
	},
	bindData : function(){
		var arrDate = this.getDaysByMonth(this.date.getYear()<1000?1900+this.date.getYear():this.date.getYear(), this.date.getMonth());
		var tds = this.dbody.getElementsByTagName("td");
		for(var i=0;i<tds.length;i++){
			tds[i].innerHTML = "";
			if(i%7 ==0){
				tds[i].className = DateChooser.classes.sunbody;
			}else if(i%7 == 6){
				tds[i].className = DateChooser.classes.satbody;
			}else{
				tds[i].className = DateChooser.classes.norbody;
			}
			if(arrDate[i] != ""){
				var a = document.createElement("a");
				a.href = "javascript:void(0)";
				a.innerHTML = arrDate[i];
				a.onclick = function(event){
							t = CommonFunctions.getElement(event);
							var d = t.innerHTML;
							this.date = new Date(this.date.getFullYear(),this.date.getMonth(),d);
							this.value = new Date(this.date.getFullYear(),this.date.getMonth(),d).format(this.dateFormatStyle)
							this.update();
							this.clickHandler();
						}.bind(this);
				tds[i].insertBefore(a,null);					
			}
			var crtdate = new Date(this.date.getFullYear(),this.date.getMonth(),arrDate[i]).format(this.dateFormatStyle);
			
			if(this.today.format(this.dateFormatStyle) == crtdate){
				tds[i].className = DateChooser.classes.todate;
			}
			if(this.value == crtdate){
				tds[i].className = DateChooser.classes.seldate;
			}			
		}	
	},
	update : function (e){
		this.year  	= this.selyear.options[this.selyear.selectedIndex].value;
		this.month 	= this.selmonth.options[this.selmonth.selectedIndex].value;
		this.date 	= new Date(this.year, this.month, 1);
		this.changeSelect();
		this.bindData();
	},
	changeSelect : function() {
		var cy = this.selyear;
		var cm = this.selmonth;
		for (var i= 0; i < cy.length; i++){
			if (cy.options[i].value == this.date.getFullYear()){
				cy[i].selected = true;
				break;
			}
		}
		for (var i= 0; i < cm.length; i++){
			if (cm.options[i].value == this.date.getMonth()){
				cm[i].selected = true;
				break;
			}
		}
	},
	attachTo :function(target){
		target.appendChild(this.container);
	},
	clickHandler:Function	
}




var DateField = Class.create(); 

DateField.language = {
	txtclear  : ["清空", "CLS"],
	txttoday  : ["今天", "TODAY"],
	txtclose  : ["关闭", "CLOSE"]
}
DateField.classes = {
	btclear:"bt_clear",
	bttoday:"bt_today",
	btclose:"bt_close"
}

DateField.prototype = {
	initialize: function(id,crtDate,fClass,dClass,beginYear,endYear,lang,islong,dateFormatStyle){
		this.id = id;
		this.lang 		= lang!=null?lang:0;
		this.input		= null;
		this.container 	= document.createElement("div");
		this.container.className = fClass;
		this.choosercontainer = document.createElement("div");		
		this.calendar 	= new DateChooser(id+"_chooser",crtDate,dClass,beginYear, endYear, lang,islong, dateFormatStyle);
		this.calendar.attachTo(this.choosercontainer);
		
		if(_isIE){
			var iframe = document.createElement("iframe");
			iframe.setAttribute("frameborder","0");
			iframe.src				="javascript:false;"
			iframe.style.cssText 	= "color:#ffffff;width:100%;background:#ffffff;border:none;filter:alpha(opacity=50);position:absolute;z-index:-1;"
			this.container.insertBefore(iframe,null);
		}
		this.container.insertBefore(this.choosercontainer,null);
		
		this.drawButtons();
		this.attachTo();
		
		this.choosercontainer.style.position = "";
		this.choosercontainer.style.left = 0;
		this.choosercontainer.style.top = 0;
		this.choosercontainer.style.width = this.container.offsetWidth + "px";
		this.container.style.display 	= "none";
		
	},
	drawButtons : function(){		
		this.btclear 	= document.createElement("input");
		this.bttoday 	= document.createElement("input");
		this.btclose 	= document.createElement("input");
		
		this.btclear.className = DateField.classes.btclear;
		this.bttoday.className = DateField.classes.bttoday;
		this.btclose.className = DateField.classes.btclose;
		
		this.btclear.type = this.bttoday.type = this.btclose.type = "button";
		
		this.btclear.value = DateField.language.txtclear[this.lang];
		this.bttoday.value = DateField.language.txttoday[this.lang];
		this.btclose.value = DateField.language.txtclose[this.lang];
		
		this.btclear.onclick = function(){this.onReset();}.bind(this);
		this.bttoday.onclick = function(){this.onToday();}.bind(this);
		this.btclose.onclick = function(){this.onClose();}.bind(this);
		
		var table 	= document.createElement("table");
		var tbody 	= document.createElement("tbody");
		var tr 		= document.createElement("tr");
		var td1 	= document.createElement("td");
		var td2 	= document.createElement("td");
		var td3 	= document.createElement("td");
		
		td1.insertBefore(this.btclear,null);
		td2.insertBefore(this.bttoday,null);
		td3.insertBefore(this.btclose,null);
		
		td1.align = td2.align = td3.align = "center";
		tr.insertBefore(td1,null);
		tr.insertBefore(td2,null);
		tr.insertBefore(td3,null);
		tbody.insertBefore(tr,null);
		table.insertBefore(tbody,null);
		table.style.width = "100%";
		this.choosercontainer.insertBefore(table,null);
		
	},
	attachTo:  function(target){
		if(target){
			target.appendChild(this.container);
		}else{
			document.body.appendChild(this.container);
		}
	},

	show : function(cvalue,target){
		this.container.style.display = "";	
		if(!!target)
			this.setPosition(target);	
		else
			this.setPosition(cvalue);	
		this.input = cvalue;
		this.calendar.value = null;
		this.calendar.date = null;		
		this.clickHandler();
		this.calendar.clickHandler = function(){this.onChoose();}.bind(this);
	},
	setPosition : function(obj){
		var po = CommonFunctions.getAbsPoint(obj);
		this.container.style.left = po.x + "px";
		this.container.style.top = (po.y + obj.offsetHeight) + "px";
	},
	clickHandler : function(){
		this.container.style.display = "";
		var crtDate = this.input.value.toDate();
		if(this.input.value!=""){
			this.calendar.date = crtDate;
			this.calendar.value = crtDate.format(this.calendar.dateFormatStyle);
		}
		else{
			this.calendar.date = new Date();
		}
		this.calendar.changeSelect();
		this.calendar.bindData();
	},
	onChoose : function(){
		this.input.value = this.calendar.value;	
		this.container.style.display = "none";
	},
	onClose : function(){
		this.container.style.display = "none";		
	},
	onReset : function(){
		this.input.value = "";	
		this.container.style.display = "none";
	},
	onToday : function(){
		this.input.value = new Date().format(this.calendar.dateFormatStyle);	
		this.container.style.display = "none";			
	}
}